<?php

namespace Arr;

class Arr
{
    /**
     * Get one or a specified number of random values from an array.
     *
     * @param array $array
     * @param int|null $number
     *
     * @return array
     * @throws \InvalidArgumentException
     */
    public static function random(array $array, int $number = null): array
    {
        $requested = null === $number ? 1 : $number;
        $count = count($array);
        if ($requested > $count) {
            throw new InvalidArgumentException(
                "You requested {$requested} items, but there are only {$count} items available."
            );
        }
        if (null === $number) {
            return $array[array_rand($array)];
        }
        if ((int) $number === 0) {
            return [];
        }
        $keys = array_rand($array, $number);
        $results = [];
        foreach ((array) $keys as $key) {
            $results[] = $array[$key];
        }

        return $results;
    }
}
